package com.rox.el;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.rox.el.MainR.DoneListener;




public class Main{

	private static final long serialVersionUID = 1L;
	private JLabel bgWord;
	private JTextField engWord;
	private JButton enterButton;
	private JButton nextButton;
	private static final int WIDTH = 320;
	private static final int HEIGHT = 224;
	private Dimension size = new Dimension(WIDTH, HEIGHT);
	private Font font = new Font("Cambria", Font.PLAIN, 20);
	private Dimension vi = new Dimension(200,20);
	private ArrayList<EnBg> cardList;
	private EnBg currentCard;
	private int currentCardIndex = 0;
	private JPanel b;
	private int imb= 0;
	private boolean hox = true;
	private ArrayList<String> wrongEn = new ArrayList<String>();
	private ArrayList<String> wrongBg = new ArrayList<String>();
	JPanel p;
	JFrame frame;
	private JButton done;
	
	//public static void main(String[] args){
	//	new Main();
	//}
	
	public Main(){

		loadCard();
		frame = new JFrame();
		frame.setTitle("Not Random");
		currentCard = cardList.get(currentCardIndex);
		currentCardIndex++;
		p = new JPanel();
		p.setBackground(Color.WHITE);
		p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
		JLabel theWord = new JLabel("The word is: ");
		theWord.setAlignmentX(Component.CENTER_ALIGNMENT);
		theWord.setSize(theWord.getPreferredSize());
		theWord.setFont(font);
		frame.add(p, BorderLayout.CENTER);
		
		bgWord = new JLabel(currentCard.bgWord);
		bgWord.setAlignmentX(Component.CENTER_ALIGNMENT);
		bgWord.setSize(theWord.getPreferredSize());
		bgWord.setFont(font);
		
		
		engWord = new JTextField();
		engWord.addActionListener(new BgListener());
		engWord.setFont(font);
		engWord.setBackground(Color.lightGray);
		engWord.setMaximumSize(vi);
		engWord.setHorizontalAlignment(JTextField.CENTER);
		engWord.setAlignmentX(Component.CENTER_ALIGNMENT);
		JPanel p2 = new JPanel();
		p2.setLayout(new BoxLayout(p2,BoxLayout.X_AXIS));
		
		enterButton = new JButton("Enter");
		enterButton.setMaximumSize(new Dimension(78,35));
		enterButton.setFont(new Font("Cambria", Font.PLAIN, 16));
		enterButton.setBorderPainted(false);
		enterButton.setFocusPainted(false);
		enterButton.setBackground(new Color(205,205,205));
		enterButton.addActionListener(new EnterListener());
		
		nextButton = new JButton("Next");
		nextButton.setMaximumSize(new Dimension(78,35));
		nextButton.setFont(new Font("Cambria", Font.PLAIN, 16));
		nextButton.setBorderPainted(false);
		nextButton.setFocusPainted(false);
		nextButton.setBackground(new Color(205,205,205));
		nextButton.addActionListener(new NextListener());
		nextButton.setVisible(false);
		
		done = new JButton("Done");
		done.setMaximumSize(new Dimension(78,35));
		done.setFont(new Font("Cambria", Font.PLAIN, 16));
		done.setBorderPainted(false);
		done.setFocusPainted(false);
		done.setBackground(new Color(205,205,205));
		done.addActionListener(new DoneListener());
		done.setVisible(false);
		
		p.add(theWord);
		p.add(bgWord);
		p.add(Box.createRigidArea(new Dimension(0, 30)));
		p.add(engWord);
		p.add(Box.createRigidArea(new Dimension(0, 35)));
		//p.add(enterButton);
		p2.add(enterButton);
		//p2.add(Box.createRigidArea(new Dimension(25,0)));
		p2.add(nextButton);
		p2.add(done);
		p.add(p2);
		b = new JPanel();
		b.setMaximumSize(new Dimension(25,25));
		b.setBackground(Color.WHITE);
		p.add(b);
		frame.setSize(size);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.setLocationRelativeTo(null);
		
		
	}
	
	private void loadCard(){
		cardList = new ArrayList<EnBg>();
		try{
			File myFile = new File("res/bat.txt");
			BufferedReader reader = new BufferedReader(new FileReader(myFile));
			String line = null; //you can combine this with a while loop (while line != null)
			while((line = reader.readLine()) != null){
				
				String[] result = line.split("/");
				EnBg card = new EnBg(result[0],result[1]);
				cardList.add(card);
				
			}
			reader.close();
			//You can also combine it with (String [] result = lineToParse.split("/");)
		}catch(Exception ex){ex.printStackTrace();}
	}
	
	class BgListener implements ActionListener{

		//WIDTH = 320;
		//HEIGHT = 224;
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(hox == true){
				if (engWord.getText().equalsIgnoreCase(currentCard.trueWord) && currentCardIndex < cardList.size()){
					nextButton.setVisible(true);
					done.setVisible(true);
					hox = false;
					imb = 0;
					enterButton.setVisible(false);
					b.setBackground(Color.GREEN);
					
				}else if(engWord.getText().equalsIgnoreCase(currentCard.trueWord) && currentCardIndex == cardList.size()){
					enterButton.setEnabled(false);
					enterButton.setText("No more");
					b.setBackground(Color.WHITE);
					new Wrong();
					Wrong.wrongWords.setText("");
					for(int i = 0; i<wrongEn.size(); i++){
						Wrong.wrongWords.append(i+1 + "." +wrongEn.get(i) + " - " + wrongBg.get(i) + "\n");
					}
					frame.setVisible(false);
					
				}else{
					b.setBackground(new Color(255/(imb + 1),0,0));
					imb++;
				}
				if (imb > 2){
					engWord.setText(engWord.getText() + " - " + currentCard.trueWord);
					//private ArrayList<String> wrongEn = new ArrayList<String>();
					//private ArrayList<String> wrongBg = new ArrayList<String>();
					wrongEn.add(currentCard.trueWord);
					wrongBg.add(currentCard.bgWord);
					imb = 0;
					if(currentCardIndex == cardList.size()){
						enterButton.setEnabled(false);
						enterButton.setText("No more");
						b.setBackground(Color.WHITE);
						new Wrong();
						Wrong.wrongWords.setText("");
						for(int i = 0; i<wrongEn.size(); i++){
							Wrong.wrongWords.append(i+1 + "." +wrongEn.get(i) + " - " + wrongBg.get(i) + "\n");
						}
						frame.setVisible(false);
					}else{
					nextButton.setVisible(true);
					done.setVisible(true);
					hox = false;
					imb = 0;
					enterButton.setVisible(false);
					b.setBackground(Color.WHITE);
					}
				}
			}else{
				nextButton.setVisible(false);
				done.setVisible(false);
				enterButton.setVisible(true);
				hox = true;
				imb = 0;
				engWord.requestFocus();
				currentCard = cardList.get(currentCardIndex);
				currentCardIndex++;
				engWord.setText("");
				bgWord.setText(currentCard.bgWord);
				b.setBackground(Color.WHITE);
			}
		}
		
	}
	class EnterListener implements ActionListener{

		//WIDTH = 320;.equalsIgnoreCase(physicist2)
		//HEIGHT = 224;
		@Override
		public void actionPerformed(ActionEvent arg0) {
				if (engWord.getText().equalsIgnoreCase(currentCard.trueWord) && currentCardIndex < cardList.size()){
					nextButton.setVisible(true);
					done.setVisible(true);
					imb = 0;
					hox = false;
					enterButton.setVisible(false);
					b.setBackground(Color.GREEN);
					engWord.requestFocus();
					
				}else if(engWord.getText().equalsIgnoreCase(currentCard.trueWord) && currentCardIndex == cardList.size()){
					enterButton.setEnabled(false);
					enterButton.setText("No more");
					b.setBackground(Color.WHITE);
					engWord.requestFocus();
					new Wrong();
					Wrong.wrongWords.setText("");
					for(int i = 0; i<wrongEn.size(); i++){
						Wrong.wrongWords.append(i+1 + "." +wrongEn.get(i) + " - " + wrongBg.get(i) + "\n");
					}
					frame.setVisible(false);
					
					
				}else{
					b.setBackground(new Color(255/(imb + 1),0,0));
					imb++;
					engWord.requestFocus();
				}
				if (imb > 2){
					engWord.setText(engWord.getText() + " - " + currentCard.trueWord);
					imb = 0;
					wrongEn.add(currentCard.trueWord);
					wrongBg.add(currentCard.bgWord);
					if(currentCardIndex == cardList.size()){
						enterButton.setEnabled(false);
						enterButton.setText("No more");
						b.setBackground(Color.WHITE);
						engWord.requestFocus();
						new Wrong();
						Wrong.wrongWords.setText("");
						for(int i = 0; i<wrongEn.size(); i++){
							Wrong.wrongWords.append(i+1 + "." +wrongEn.get(i) + " - " + wrongBg.get(i) + "\n");
						}
						frame.setVisible(false);
					}else{
					nextButton.setVisible(true);
					done.setVisible(true);
					imb = 0;
					hox = false;
					enterButton.setVisible(false);
					b.setBackground(Color.WHITE);
					engWord.requestFocus();
					}
				}
		}
		
	}
	
	class DoneListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			String text;
			new Wrong();
			Wrong.wrongWords.setText("");
			for(int i = 0; i<wrongEn.size(); i++){
				text = Wrong.wrongWords.getText();
				if(!text.contains(wrongEn.get(i))){
				Wrong.wrongWords.append(i+1 + "." +wrongEn.get(i) + " - " + wrongBg.get(i) + "\n");
				}else continue;
			}
			frame.setVisible(false);
			
		}
		
	}
	
	class NextListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			nextButton.setVisible(false);
			done.setVisible(false);
			enterButton.setVisible(true);
			imb = 0;
			hox = true;
			engWord.requestFocus();
			currentCard = cardList.get(currentCardIndex);
			currentCardIndex++;
			engWord.setText("");
			bgWord.setText(currentCard.bgWord);
			b.setBackground(Color.WHITE);
			
			
		}
		
	}
	
	
	
}
